package com.focusai.efairy.ui.activity.project;

import android.os.Bundle;
import android.support.v4.view.MenuItemCompat;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;

import com.focusai.efairy.R;
import com.focusai.efairy.model.request.AddGmToProjectReqeust;
import com.focusai.efairy.network.HttpManager;
import com.focusai.efairy.network.Response;
import com.focusai.efairy.network.exception.NetworkException;
import com.focusai.efairy.ui.base.SwipeBackBaseActivity;
import com.focusai.efairy.utils.PhoneCheckUtils;

/**
 * Created by luo_guangwen on 2017/9/18.
 */

public class ProjectAddGmActivity extends SwipeBackBaseActivity{
    public static final String KEY_PROJECT_ID = "key_project_id";

    private EditText editphone;
    private EditText editName;
    private String projectId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_add_gm_user);
        initView();
        if (getIntent() != null){
            projectId = getIntent().getStringExtra(KEY_PROJECT_ID);
        }
    }

    @Override
    public void initTitte() {
        setCenterTitleName("新增管理员");
    }

    @Override
    protected void initView() {
        editName = findView(R.id.text_user_name);
        editphone = findView(R.id.text_user_phone);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuItem menuItem1 = menu.add(MENU_GROUP_ID, MENU_ITEM_ADD, 0, R.string.save).setTitle(R.string.save);
        MenuItemCompat.setShowAsAction(menuItem1, MenuItemCompat.SHOW_AS_ACTION_ALWAYS);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == MENU_ITEM_ADD) {
            saveUser();
        }
        return super.onOptionsItemSelected(item);
    }

    private void saveUser(){
        if (TextUtils.isEmpty(editName.getText().toString())) {
            showToast("请输入用户名称");
            return;
        }
        if (TextUtils.isEmpty(editphone.getText().toString())) {
            showToast("请输入用户手机号");
            return;
        }
        if (!PhoneCheckUtils.isChinaPhoneLegal(editphone.getText().toString())) {
            showToast("请输入正确手机号");
            return;
        }
        showSingleDialog("正在保存数据..");
        HttpManager.getInstance().sendRequest(new AddGmToProjectReqeust(projectId, editphone.getText().toString(), editName.getText().toString(),
                new Response.Listener<String>() {
                    @Override
                    protected void onSuccess(String response) {
                        closeSingleDialog();
                        showToast("保存数据成功");
                        setResult(RESULT_OK);
                        finish();
                    }

                    @Override
                    protected void onFail(NetworkException exception) {
                        closeSingleDialog();
                        doException(exception);
                    }
                }));
    }
}
